package com.qdxwx.web.controller.live;


import com.qdxwx.common.cache.CacheUtils;
import com.qdxwx.common.constants.CacheConstants;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.ObjectUtils;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.data.service.advert.AdvertService;
import com.qdxwx.data.service.category.CategoryService;
import com.qdxwx.data.service.course.CourseNodeService;
import com.qdxwx.data.service.course.CourseService;
import com.qdxwx.data.service.live.LiveCourseService;
import com.qdxwx.data.service.teacher.TeacherService;
import com.qdxwx.models.course.Course;
import com.qdxwx.models.course.CourseDTO;
import com.qdxwx.models.course.CourseNode;
import com.qdxwx.models.course.CourseQuery;
import com.qdxwx.models.teacher.Teacher;
import com.qdxwx.web.controller.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import jakarta.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 直播接口
 * @author developer
 */
@Controller
@RequestMapping("/live")
public class LiveIndexController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(LiveIndexController.class);
	@Autowired
	private CourseService courseService;
	@Autowired
	private TeacherService teacherService;
	@Autowired
	private CategoryService categoryService;
	@Autowired
	private CourseNodeService courseNodeService;
	@Autowired
	private LiveCourseService liveCourseService;
	@Autowired
	private AdvertService advertService;

	/**
	 * 直播首页
	 */
	@GetMapping("/index.html")
	public String liveIndex(HttpServletRequest request, ModelMap model) {
		//近期直播章节
		CourseNode courseNode = new CourseNode();
		courseNode.setFileType("LIVE");//直播
		courseNode.setNodeType(1);//节点类型 0文件目录 1视频
		courseNode.setQueryOrder("near");//近期
		courseNode.setIsAvailable(1);
		courseNode.setLiveEndTime(LocalDateTime.now());//包括今天之后的

		model.put("courseDtos", courseNodeService.getNearestNodeList(courseNode, 4));
		// 获得推荐直播课程
		List<CourseDTO> liveRecommend = courseService.getDTOListByAlbumId(2);
		if (ObjectUtils.isNotNull(liveRecommend)) {
			for (CourseDTO courseDto : liveRecommend) {
				courseDto.setCourseNodeList(liveCourseService.getNextByCourseId(courseDto.getCourseId()));
			}
		}
		request.setAttribute("liveRecommend", liveRecommend);

		CourseQuery courseQuery;
		//最新直播
		@SuppressWarnings("unchecked")
		List<Course> newCourseList = (List<Course>) CacheUtils.get(CacheConstants.INDEX_LIVE_RECOMMEND_PREFIX + "new");
		if (ObjectUtils.isNull(newCourseList)) {
			courseQuery = new CourseQuery();
			//只查询上架的
			courseQuery.setIsAvailable(1);
			//查询直播课程
			courseQuery.setSellType("LIVE");
			//排序 最新
			courseQuery.setOrderBy(5);
			// 搜索列表
			newCourseList = courseService.getList(courseQuery, new PageRequest(1, 8));
			CacheUtils.set(CacheConstants.INDEX_LIVE_RECOMMEND_PREFIX + "new", newCourseList, CacheConstants.RECOMMEND_COURSE_TIME);
		}
		model.put("newCourseList", newCourseList);
		// 查询所有1级专业

		model.put("categoryRoots", categoryService.getChildren(0));
		//直播免费排行
		newCourseList = (List<Course>) CacheUtils.get(CacheConstants.INDEX_LIVE_RECOMMEND_PREFIX + "free");
		if (ObjectUtils.isNull(newCourseList)) {
			courseQuery = new CourseQuery();
			//只查询上架的
			courseQuery.setIsAvailable(1);
			//查询直播课程
			courseQuery.setSellType("LIVE");
			//排序 浏览数多的
			courseQuery.setOrderBy(1);
			//免费
			courseQuery.setIsFree(1);
			// 搜索列表
			newCourseList = courseService.getList(courseQuery, new PageRequest(1, 6));
			//查询直播章节
			for (Course courseDto : newCourseList) {
				courseDto.setCourseNodeList(liveCourseService.getNextByCourseId(courseDto.getCourseId()));
			}
			CacheUtils.set(CacheConstants.INDEX_LIVE_RECOMMEND_PREFIX + "free", newCourseList, CacheConstants.RECOMMEND_COURSE_TIME);
		}
		model.put("freeCourseList", newCourseList);

		//直播免费排行
		newCourseList = (List<Course>) CacheUtils.get(CacheConstants.INDEX_LIVE_RECOMMEND_PREFIX + "charge");
		if (ObjectUtils.isNull(newCourseList)) {
			courseQuery = new CourseQuery();
			//只查询上架的
			courseQuery.setIsAvailable(1);
			//查询直播课程
			courseQuery.setSellType("LIVE");
			//排序 浏览数多的
			courseQuery.setOrderBy(1);
			//收费
			courseQuery.setIsFree(0);
			// 搜索列表
			newCourseList = courseService.getList(courseQuery, new PageRequest(1, 6));
			//查询直播章节
			for (Course course : newCourseList) {
				course.setCourseNodeList(liveCourseService.getNextByCourseId(course.getCourseId()));
			}
			CacheUtils.set(CacheConstants.INDEX_LIVE_RECOMMEND_PREFIX + "charge", newCourseList, CacheConstants.RECOMMEND_COURSE_TIME);
		}
		model.put("chargeCourseList", newCourseList);

		// 查询课程购买多的几位讲师
		@SuppressWarnings("unchecked")
		List<Teacher> teacherList = (List<Teacher>) CacheUtils.get(CacheConstants.INDEX_TEACHER_RECOMMEND + "_live");
		if (ObjectUtils.isNull(teacherList)) {
			courseQuery = new CourseQuery();
			//查询直播课程
			courseQuery.setSellType("LIVE");
			//排序 购买数多的
			courseQuery.setOrderBy(2);
			teacherList = teacherService.getListByCourse(courseQuery, 4);
			CacheUtils.set(CacheConstants.INDEX_TEACHER_RECOMMEND + "_live", teacherList, CacheConstants.RECOMMEND_COURSE_TIME);//设置缓存
		}
		model.put("teacherList", teacherList);

		//List<Advert> AdvertBanner = advertService.getList(0, 1, 3);


		return getViewPath("live/live-index");
	}

	/**
	 * 直播首页 按分类推荐
	 */
	@RequestMapping("/ajax/recommended.html")
	public String liveRecommended(HttpServletRequest request, ModelMap mmap) {
		int categoryId = TKConvert.toInteger(request.getParameter("categoryId"));
		@SuppressWarnings("unchecked")
		List<Course> courseList = (List<Course>) CacheUtils.get(CacheConstants.INDEX_LIVE_RECOMMEND_PREFIX + categoryId);
		if (ObjectUtils.isNull(courseList)) {
			CourseQuery courseQuery = new CourseQuery();
			courseQuery.setCategoryId(categoryId);
			//只查询上架的
			courseQuery.setIsAvailable(1);
			//查询直播课程
			courseQuery.setSellType("LIVE");
			// 搜索列表
			courseList = courseService.getList(courseQuery, new PageRequest(1, 5));
			CacheUtils.set(CacheConstants.INDEX_LIVE_RECOMMEND_PREFIX + categoryId, courseList, CacheConstants.RECOMMEND_COURSE_TIME);
		}
		mmap.put("courseList", courseList);
		mmap.put("categoryId", categoryId);
		return getViewPath("live/live-index-recommend");
	}
}